
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_content_item.
	''' </summary>
	Public Partial Class view_content_item
		Inherits BaseWebPage
		Protected lblColumn3 As System.Web.UI.WebControls.Label
		Protected id As Integer = -1
		Protected usr As User
		Protected contentItem As ContentItem
		Protected filename As String = ""
		Protected lblColumn2 As System.Web.UI.WebControls.Label
		'protected System.Web.UI.WebControls.Label lblColumn1;
		Protected htPageConstants As New Hashtable()
		Protected numContentItems As Integer = Constants.MUSIC_CONTENTCOLUMN_MAX
		Protected mediaControlHeight As Integer = 0
		Protected pageID As Integer = -1
		Protected period As DateTime = DateTime.Now

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			usr = LoggedUser

			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			If Request.Params("id") = Nothing OrElse Request.Params("id") = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			' init page from XML
			Dim htPageConstants As New Hashtable()

			Me.initPageTitles(Constants.VIEW_CONTENT_ITEM, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucMusicTabs.ActiveTab = 4
			ucMusicTabs.PageName = Constants.VIEW_CONTENT_ITEM

			id = Convert.ToInt32(Request.Params("id"))
			contentItem = ContentItem.GetContentItemByID(id)
			pageID = contentItem.SpecialPageID
			Dim specialPage As SpecialPage = SpecialPages.GetSpecialPageByID(pageID)
			' init item top table
			' init labels
			lblPageTitle.Text = specialPage.Name

			' init values
			lblColumn3Value.Text = contentItem.Content

			If contentItem.UserID <> -1 Then
				Dim contentItemUser As User = Users.GetUserByID(contentItem.UserID)
				lblUserName.Text = contentItemUser.Name
				hlColumn1Value.Text = contentItemUser.Name
				hlColumn1Value.NavigateUrl = Users.ComposeMemeberLink(contentItemUser)
				'"../" + Constants.MEMBERS + "?id=" + contentItem.UserID;
				hlColumn1Value.Attributes.Add("onclick", Users.ComposeMemeberLinkOnClickJS(contentItemUser, trUserCanceledMessage))
				If contentItemUser.DefaultPicture <> Nothing Then
					hlUserGroup.NavigateUrl = Users.ComposeMemeberLink(contentItemUser)
					'"../" + Constants.MEMBERS + "?id=" + contentItem.UserID;
					hlUserGroup.Attributes.Add("onclick", Users.ComposeMemeberLinkOnClickJS(contentItemUser, trUserCanceledMessage))
					hlUserGroup.ImageUrl = Pictures.GetPictureSrcFullURL(contentItemUser.DefaultPicture, True, "../")
				Else
					hlUserGroup.ImageUrl = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
				End If
				hlUserGroup.ToolTip = contentItemUser.Name
			End If

			If contentItem.GroupID <> -1 Then
				Dim contentItemGroup As Group = Groups.GetGroupByID(contentItem.GroupID)
				lblUserName.Text = contentItemGroup.Name
				hlColumn1Value.Text = contentItemGroup.Name
				hlColumn1Value.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + contentItem.GroupID
				If contentItemGroup.DefaultPicture <> Nothing Then
					hlUserGroup.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + contentItem.GroupID
					hlUserGroup.ImageUrl = Pictures.GetPictureSrcFullURL(contentItemGroup.DefaultPicture, True, "../")
				Else
					hlUserGroup.ImageUrl = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
				End If
				hlUserGroup.ToolTip = contentItemGroup.Name
			End If

			hlUserGroup.CssClass = "small_image"

			' init media player file
			Dim tmpLinkType As New LinkTypes.LinkType(-1, "")
			If contentItem.LinkTypeID <> -1 Then
				tmpLinkType = DirectCast(Constants.LINKTYPES.GetLinkTypeByID(contentItem.LinkTypeID), LinkTypes.LinkType)
			End If

			Select Case tmpLinkType.Name
				Case Constants.LINK_TYPE_CLIP
					filename = contentItem.LinkURL
					hlItemLink.Visible = False
					mediaControlHeight = Constants.PLAYER_VIDEO_HEIGHT
					panMediaPlayer.Visible = True
					Exit Select
				Case Constants.LINK_TYPE_SONG
					filename = contentItem.LinkURL
					hlItemLink.Visible = False
					'hlItemLink.NavigateUrl = "../#";
					'hlItemLink.Attributes.Add("onclick", constructMusicURL(Server.UrlEncode(filename)));
					mediaControlHeight = Constants.PLAYER_AUDIO_HEIGHT
					panMediaPlayer.Visible = True
					Exit Select
				Case Constants.LINK_TYPE_IMAGE
					lblColumn2Value.Visible = False
					hlItemLink.NavigateUrl = "../#"
					hlItemLink.Attributes.Add("onclick", Me.constructImageURL(contentItem.LinkURL))
					panMediaPlayer.Visible = False
					Exit Select
				Case Constants.LINK_TYPE_PAGE
					lblColumn2Value.Visible = False
					If Not contentItem.LinkURL.ToLower().StartsWith(Constants.APPLICATION_URL.ToLower()) Then
						hlItemLink.Target = "_blank"
					Else
						hlItemLink.Target = "_self"
					End If

					hlItemLink.NavigateUrl = contentItem.LinkURL
					panMediaPlayer.Visible = False
					Exit Select
				Case Else
					lblColumn2Value.Visible = False
					panMediaPlayer.Visible = False
					Exit Select
			End Select

			If contentItem.LinkName.Length > 0 Then
				hlItemLink.Text = HttpUtility.HtmlEncode(contentItem.LinkName)
				lblColumn2Value.Text = HttpUtility.HtmlEncode(contentItem.LinkName)
			Else
				hlItemLink.Text = contentItem.LinkURL
				lblColumn2Value.Text = contentItem.LinkURL
			End If

			period = Common.DateUtilities.GetStartOfQuarter(contentItem.ItemDate.Year, Common.DateUtilities.GetQuarter(DirectCast(contentItem.ItemDate.Month, Common.Month)))
			' Bind data
			If Not IsPostBack Then
				BindGrid()
				BindLeftGrid()
			End If
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim commentsList As ArrayList = contentItem.Comments()
			Dim dt As New DataTable()
			Dim startIndex As Integer = 0
			Dim endIndex As Integer = -1

			endIndex = startIndex + commentsList.Count

			dt.Columns.Add(New DataColumn("SenderID", GetType(Integer)))
			dt.Columns.Add(New DataColumn("SenderName", GetType(String)))
			dt.Columns.Add(New DataColumn("SenderImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Date", GetType(String)))
			dt.Columns.Add(New DataColumn("Time", GetType(String)))
			dt.Columns.Add(New DataColumn("Comment", GetType(String)))
			dt.Columns.Add(New DataColumn("CommentID", GetType(String)))
			dt.Columns.Add(New DataColumn("ButtonsVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))
			dt.Columns.Add(New DataColumn("SenderNameFull", GetType(String)))

			If commentsList.Count = 0 Then
				dgComments.Visible = False
				lblNoComments.Visible = True
			Else
				Dim curCommentDate As DateTime
				Dim dr As DataRow
				Dim temp As UserPicture
				Dim curSender As User
				Dim liComment As ContentItem.ContentItemComment

				' List user's inbox messages
				Dim i As Integer = startIndex
				While i < endIndex
					dr = dt.NewRow()

					endIndex = Math.Min(endIndex, commentsList.Count)
					dr = dt.NewRow()
					liComment = DirectCast(commentsList(i), ContentItem.ContentItemComment)
					curCommentDate = liComment.[Date]
					curSender = Users.GetUserByID(liComment.PostedByUserID)

					dr(0) = liComment.PostedByUserID
					dr(1) = getBeginning(HttpUtility.HtmlEncode(curSender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					temp = curSender.DefaultPicture
					If temp <> Nothing Then
						dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
					End If

					dr(3) = Users.ComposeMemeberLink(curSender)
					dr(9) = Users.ComposeMemeberLinkOnClickJS(curSender, trUserCanceledMessage)

					dr(4) = curCommentDate.DayOfWeek + ", " + Constants.MONTHS(curCommentDate.Month - 1) + " " + curCommentDate.Day + ", " + curCommentDate.Year
					dr(5) = curCommentDate.ToShortTimeString()
					dr(6) = showLinksInMessage(liComment.Comment, False)
					dr(7) = liComment.ID
					If LoggedUser.Status = Constants.appAdmin Then
						dr(8) = True
					Else
						dr(8) = False
					End If

					dr(10) = curSender.Name

					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			dgComments.DataSource = CreateDataSource()
			dgComments.DataBind()
		End Sub

		Protected Function CreateLeftDataSource() As ICollection
			Dim contentItems As ArrayList = SpecialPage.GetContentItems(pageID, period, -1, -1, -1)
			' DateTime.Now, Constants.MUSIC_ITEM_LC_MAX, contentItem.UserID, contentItem.GroupID);
			Dim dt As New DataTable()

			Dim contentItemsCount As Integer = contentItems.Count
			dgContentItems.AllowSorting = True

			If contentItemsCount <= 0 Then
				lblNoContentItems.Visible = True
				dgContentItems.Visible = False
			Else

				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("MemberName", GetType(String)))
				dt.Columns.Add(New DataColumn("URL", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("ItemURL", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemName", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemGenreName", GetType(String)))
				dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))

				' List left items
				Dim user As User = Nothing
				Dim grp As Group = Nothing
				Dim tmpLinkType As New LinkTypes.LinkType(-1, "")

				For Each result As ContentItem In contentItems
					dr = dt.NewRow()

					If result.UserID > 0 Then
						user = BO.Users.GetUserByID(result.UserID)
						dr(0) = Me.getBeginning(HttpUtility.HtmlEncode(user.Name), Constants.MAX_NAME_LENGTH_ABOVE_MUSIC_TRACKS)
ElseIf result.GroupID > 0 Then
						grp = BO.Groups.GetGroupByID(result.GroupID)
						dr(0) = Me.getBeginning(HttpUtility.HtmlEncode(grp.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					End If
					dr(1) = "../" + Constants.VIEW_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID
					'"../" + Constants.MEMBERS + "?id=" + user.ID;
					dr(2) = result.ID
					dr(3) = "../" + Constants.VIEW_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID
					dr(4) = HttpUtility.HtmlEncode(result.LinkName)
					If result.LinkName = "" AndAlso result.LinkURL <> "" Then
						dr(4) = result.LinkURL
					End If

					dr(5) = result.Content
					dr(6) = "return"

					dt.Rows.Add(dr)
				Next
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindLeftGrid()
			'init datagrid with database data ordered by "orederBy" param
			dgContentItems.DataSource = CreateLeftDataSource()
			dgContentItems.DataBind()
		End Sub
		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgComments.ItemCommand, AddressOf Me.dgComments_ItemCommand

		End Sub
#End Region

		Protected Sub btnPostComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim comment As String = ""

				comment = Server.HtmlDecode(txtComment.Text.Trim())
				comment = comment.Substring(0, Math.Min(comment.Length, Constants.TEXT_AREA_MESSAGES_MAX_CHARS))

				trItem.Visible = False
				trConfirmItem.Visible = True
				lblComment.Text = comment
				lblPageTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_ConfirmTitle_TEXT")
			End If
		End Sub

		Protected Sub btnPost_Click(ByVal sender As Object, ByVal e As System.EventArgs)

			Dim comment As String = lblComment.Text
			Dim liComment As ContentItem.ContentItemComment = Nothing

			liComment = New ContentItem.ContentItemComment(id, usr.ID, comment)
			liComment.Save()
			Response.Redirect(Constants.VIEW_CONTENT_ITEM + "?id=" + id + "&" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID)
		End Sub

		Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			trItem.Visible = True
			trConfirmItem.Visible = False
		End Sub

		Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(Constants.VIEW_CONTENT_ITEM + "?id=" + id + "&" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID)
		End Sub

		Private Sub dgComments_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			If cmd = "Delete" Then
				Dim liComment As ContentItem.ContentItemComment = ContentItem.GetCommentByID(Convert.ToInt32(e.CommandArgument))
				If liComment <> Nothing Then
					liComment.DoDelete()
					BindGrid()
				End If
			End If
		End Sub

	End Class
End Namespace






